import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/util/auth'
// import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  account: '',
  depart: '',
  id_depart: 0,
  role: '',
  id_role: 0,
  uid: 0,
  index_column: []
}
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_ACCOUNT: (state, account) => {
    state.account = account
  },
  SET_DEPART: (state, depart) => {
    state.depart = depart
  },
  SET_ID_DEPART: (state, id_depart) => {
    state.id_depart = id_depart
  },
  SET_ROLE: (state, role) => {
    state.role = role
  },
  SET_UID: (state, uid) => {
    state.uid = uid
  },
  SET_ID_ROLE: (state, id_role) => {
    state.id_role = id_role
  },
  SET_INDEX_COLUMN: (state, index_column) => {
    state.index_column = index_column
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, captcha, captchaID } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password, captcha: captcha, captchaID: captchaID }).then(response => {
        const { data } = response

        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    console.log({ commit, state });
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        let { data } = response
        // console.log(state)
        // data = {
        //   // roles: ['admin'],
        //   uid: 13,
        //   role: '管理员',
        //   id_role: 1,
        //   depart: '运营一部',
        //   name: '张三',
        //   account: 'admin@163.com'
        // }
        if (!data) {
          reject('Verification failed, please Login again.')
        }
        const { account, depart, name, role, uid, id_role, id_depart, index_column } = data
        commit('SET_NAME', name)
        commit('SET_ACCOUNT', account)
        commit('SET_DEPART', depart)
        commit('SET_ID_DEPART', id_depart)
        commit('SET_ROLE', role)
        commit('SET_UID', uid)
        commit('SET_ID_ROLE', id_role)
        commit('SET_INDEX_COLUMN', index_column)
        resolve(data)

        console.log(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        removeToken()
        // resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

